<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="hAdd">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="启用"
          width="180"
        >
          <template slot-scope="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>{{ row.state?'已启用':'未启用' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="hOk(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="hPrem(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="hEdit(row)">编辑</el-button>

              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          :total="total"
          :page-sizes="pagesizes"
          :page-size="pagesize"
          :current-page="page"
          layout="sizes,prev, pager, next"
          @current-change="hChange"
          @size-change="hSizeChange"
        />
      </el-row>
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="hClose">
      <!-- 表单内容 -->
      <el-form ref="formData" :rules="rules" :model="roleForm" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item prop="state" label="启用">
          <el-switch
            v-model="roleForm.state"
            :active-value="1"
            :inactive-value="0"
            size="mini"
          />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label-width="75px">
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="hSave">确定</el-button>
              <el-button size="mini" @click="hClose">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <AssignPermission ref="rolePerm" :show-permission.sync="showPermission" :cur-id="curId" />
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, delRole } from '@/api/role'
import AssignPermission from './components/assignPermission.vue'
export default {
  name: 'Role',
  components: {
    AssignPermission
  },
  data() {
    return {
      curId: -1,
      showPermission: false,
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [
          { required: true, message: '角色名称必填', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述必填', trigger: 'blur' }
        ]
      },
      list: [],
      total: 0,
      pagesizes: [5, 6, 7, 8, 9, 10],
      pagesize: 10,
      page: 1,
      showDialog: false
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    hPrem(id) {
      this.showPermission = true
      this.curId = id
      this.$nextTick(() => {
        this.$refs.rolePerm.getRolePrem()
      })
    },
    async hDel(id) {
      await delRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) this.page--
      }
      this.getRoleList()
    },
    async hOk(row) {
      if (!row.name || !row.description) return this.$message.success('请输入内容')
      await updateRole(row.editRow)
      this.$message.success('更新成功')
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
      this.getRoleList()
    },
    async doAdd() {
      await addRole(this.roleForm)
      // for (let i = 0; i < 100; i++) {
      //   await addRole({
      //     name: '前端90期之神兰家辉到此一游' + i,
      //     description: '111',
      //     state: 1
      //   })
      // }
      this.$message.success('添加成功')
      this.page = Math.ceil((this.total++) / this.pagesize)
      this.showDialog = false
      this.$refs.formData.resetFields()
      this.getRoleList()
    },
    hSave() {
      this.$refs.formData.validate(valid => {
        if (valid) this.doAdd()
      })
    },
    hAdd() {
      this.showDialog = true
    },
    async getRoleList() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      this.list = res.data.rows
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.total = res.data.total
    },
    hChange(page) {
      this.page = page
      this.getRoleList()
    },
    hSizeChange(pagesize) {
      this.pagesize = pagesize
      this.page = 1
      this.getRoleList()
    },
    hEdit(row) {
      row.isEdit = true
    },
    hClose() {
      this.showDialog = false
      this.$refs.formData.resetFields()
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
